//
//  XMFindView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/7/2.
//

#import "XMFindView.h"
#import "ZMYTabBar.h"
#import "WaterfallFlowLayout.h"
#import "CustomCollectionViewCell.h"
#import "XMPicwordCell.h"

@interface XMFindView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, ZMYTabBarDelegate, WaterfallFlowLayoutDelegate>

@property (nonatomic, strong) ZMYTabBar *tabBar;
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *picwordModelArr;

@end

@implementation XMFindView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xffffff);
    
    NSArray *tabTitles = @[@"推荐", @"关注", @"同城"];
    self.tabBar = [[ZMYTabBar alloc] initWithFrame:CGRectMake(KScaleWidth(90.0), kNavagationBarH - 44, self.width - KScaleWidth(180.0), 44) titles:tabTitles];
    self.tabBar.delegate = self;
    [self.navView addSubview:self.tabBar];
    
//    self.imageHeights = @[@(100), @(200), @(150), @(250), @(120), @(180), @(220), @(160)];
    // 创建自定义布局
    [self addSubview:self.collectionView];
    
    UIView * searchView = [UIView new];
    searchView.frame = CGRectMake(0, kNavagationBarH - 44, 44, 44);
    [self.navView addSubview:searchView];
     UIButton * searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(10, 0, 40, 40);
    searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [searchButton setImage:[UIImage imageNamed:@"search_remend"] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:searchButton];
    
    UIButton * cameButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cameButton.frame = CGRectMake(self.width - 44, kNavagationBarH - 44, 44, 44);
    cameButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [cameButton setImage:[UIImage imageNamed:@"came"] forState:UIControlStateNormal];
    [self.navView addSubview:cameButton];
    [cameButton addTarget:self action:@selector(requestPhotoLibraryAccess:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setContentWithPicwordModelArr:(NSArray *)picwordModelArr
                                 page:(NSInteger)page {
    [_collectionView.mj_header endRefreshing];
    if (page == 1) {
        [self.picwordModelArr removeAllObjects];
    }
    if (picwordModelArr.count < 10) {
        [_collectionView.mj_footer endRefreshingWithNoMoreData];
    }
    else {
        [_collectionView.mj_footer endRefreshing];
    }
    [self.picwordModelArr addObjectsFromArray:picwordModelArr];
    [_collectionView reloadData];
}

#pragma mark - BtnAction
- (void)requestPhotoLibraryAccess:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(picWordPublishAction)]) {
        [self.delegate picWordPublishAction];
    }
}

- (void)searchAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(picWordSearchAction)]) {
        [self.delegate picWordSearchAction];
    }
}

- (void)ZMYTabBar:(nonnull ZMYTabBar *)tabBar didSelectTabAtIndex:(NSInteger)index {
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabSelect:)]) {
        [self.delegate tabSelect:index];
    }
}

#pragma mark - Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _picwordModelArr.count;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMPicwordModel *picwordModel = self.picwordModelArr[indexPath.item];
    // 计算单行文本宽度
    CGFloat height = [picwordModel.title getHeightWithFont:KScaleWidth(16.0) withWidth:(self.width - 30)/2 - KScaleWidth(10.0)*2] + KScaleWidth(5.0);
    return KScaleWidth(210.0) + KScaleWidth(10.0) + height + KScaleWidth(44.0); // 默认占位尺寸
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMPicwordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMPicwordCellName" forIndexPath:indexPath];
    XMPicwordModel *picwordModel = _picwordModelArr[indexPath.row];
    [cell setContentWithPicwordModel:picwordModel];
    XMWeakSelf;
    cell.likeBlock = ^{
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(picwordLikedWithPicwordId:)]) {
            [weakSelf.delegate picwordLikedWithPicwordId:picwordModel.objectId];
        }
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatPicwordLikedWithPicwordId:)]) {
        XMPicwordModel *picwordModel = _picwordModelArr[indexPath.row];
        [self.delegate chatPicwordLikedWithPicwordId:picwordModel.objectId];
    }
}

#pragma mark - Init
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        WaterfallFlowLayout *layout = [[WaterfallFlowLayout alloc] init];
        layout.delegate = self;
        layout.columnCount = 2;
        layout.columnSpacing = 10;
        layout.rowSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        // 创建 UICollectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = kColorHEX(0xF0F0F0);
        [_collectionView registerClass:[XMPicwordCell class] forCellWithReuseIdentifier:@"XMPicwordCellName"];
        layout.sectionInset = UIEdgeInsetsMake(0, layout.sectionInset.left, layout.sectionInset.bottom, layout.sectionInset.right);
        
        XMWeakSelf;
        _collectionView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshData)]) {
                [weakSelf.delegate refreshData];
            }
        }];
        _collectionView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(loadMore)]) {
                [weakSelf.delegate loadMore];
            }
        }];
        
    }
    return _collectionView;
}

- (NSMutableArray *)picwordModelArr {
    if (!_picwordModelArr) {
        _picwordModelArr = [NSMutableArray array];
    }
    return _picwordModelArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
